package com.zofer.study;

import java.util.concurrent.*;

public class FutureCallableTest {
    static ExecutorService poolService = Executors.newCachedThreadPool();

    static class Task implements Callable<Integer> {
        int count;
        public Task(int count) {
            this.count = count;
        }

        @Override
        public Integer call() {
            try {
                System.out.println(Thread.currentThread().getName()+"正在执行...");
                Thread.sleep(3000);
                System.out.println(Thread.currentThread().getName()+"执行完了");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return count;
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        for (int i = 0; i < 10 ; i++) {
            Task t1 = new Task(i);
            Future<Integer> future = poolService.submit(t1);
            System.out.println("线程返回"+i+"："+future.get());
        }
        System.out.println("主线程:"+Thread.currentThread().getName());
    }
}
